package com.lxmes.sales.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.lxmes.base.domain.JcItembzinfo;
import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.common.constant.Constants;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.sales.domain.NcClientFiles;
import com.lxmes.sales.domain.NcContract;
import com.lxmes.sales.mapper.NcContractMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.sales.mapper.NcQuotedPriceMapper;
import com.lxmes.sales.domain.NcQuotedPrice;
import com.lxmes.sales.service.INcQuotedPriceService;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 报价单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-26
 */
@Service
public class NcQuotedPriceServiceImpl implements INcQuotedPriceService
{
    @Autowired
    private NcQuotedPriceMapper ncQuotedPriceMapper;
    @Autowired
    private NcContractMapper ncContractMapper;
    @Autowired
    protected Validator validator;



    /**
     * 查询报价单
     *
     * @param id 报价单主键
     * @return 报价单
     */
    @Override
    public NcQuotedPrice selectNcQuotedPriceById(Long id)
    {
        return ncQuotedPriceMapper.selectNcQuotedPriceById(id);
    }

    @Override
    public NcQuotedPrice selectNcQuotedPriceBybjdH(String bjdH)
    {
        List<NcQuotedPrice> ncQuotedPrices = ncQuotedPriceMapper.selectNcQuotedPriceBybjdH(bjdH);
        NcQuotedPrice result = new NcQuotedPrice();
        result.setBjdH(ncQuotedPrices.get(0).getBjdH());
        result.setBjyxsj(ncQuotedPrices.get(0).getBjyxsj());
        result.setUnit(ncQuotedPrices.get(0).getUnit());
        result.setBjr(ncQuotedPrices.get(0).getBjr());
        result.setBjDate(ncQuotedPrices.get(0).getBjDate());
        List<JcItembzinfo> infoList = new ArrayList<>();
        for(NcQuotedPrice bjd : ncQuotedPrices){
            JcItembzinfo jcItembzinfo = new JcItembzinfo();
            jcItembzinfo.setSpec(bjd.getSpecification());
            infoList.add(jcItembzinfo);
            result.setInfoList(infoList);
        }
        return result;
    }

    /**
     * 查询报价单列表
     *
     * @param ncQuotedPrice 报价单
     * @return 报价单
     */
    @Override
    public List<NcQuotedPrice> selectNcQuotedPriceList(NcQuotedPrice ncQuotedPrice)
    {
        List<NcQuotedPrice> ncQuotedPrices = ncQuotedPriceMapper.selectNcQuotedPriceList(ncQuotedPrice);
        return ncQuotedPrices;
    }

    @Override
    public List<NcQuotedPrice> selectNcQuotedPriceListByMaterial(NcQuotedPrice ncQuotedPrice) {
        return ncQuotedPriceMapper.selectNcQuotedPriceListByMaterial(ncQuotedPrice);
    }

    /**
     * 新增报价单
     *
     * @param ncQuotedPrice 报价单
     * @return 结果
     */
    @Override
    public int insertNcQuotedPrice(NcQuotedPrice ncQuotedPrice)
    {

        List<JcItembzinfo> itembzinfoLists = ncQuotedPrice.getInfoList();
        for(JcItembzinfo jcItembzinfo : itembzinfoLists){
            ncQuotedPrice.setSpecification(jcItembzinfo.getSpec());
            ncQuotedPrice.setOutDia(jcItembzinfo.getFlwjD()+"");
            ncQuotedPrice.setInnerDia(jcItembzinfo.getFlnj()+"");
            ncQuotedPrice.setFlThick(jcItembzinfo.getFlhdC()+"");
            if(StringUtils.isNull(ncQuotedPrice.getBjdH())){
                ncQuotedPrice.setBjdH("TKBJ"+NewNumber.createNewNumber(ncQuotedPriceMapper.getBjCode(DateUtils.getDate())));
            }
            ncQuotedPrice.setJstj(jcItembzinfo.getJstj()+"");
            ncQuotedPrice.setJszl(jcItembzinfo.getJszl()+"");
            ncQuotedPrice.setClcbj(jcItembzinfo.getClcbj()+"");
            ncQuotedPrice.setJgcbj(jcItembzinfo.getJgcbj()+"");
            ncQuotedPrice.setMfccbj(jcItembzinfo.getMfccbj()+"");
            ncQuotedPrice.setHjcbj(jcItembzinfo.getHjcbj()+"");
            ncQuotedPrice.setLlbj(jcItembzinfo.getLlbj()+"");
            ncQuotedPrice.setSjbj(jcItembzinfo.getSjbj()+"");
            ncQuotedPrice.setTaxRate(jcItembzinfo.getTaxRate().floatValue());
            ncQuotedPrice.setHsbj(jcItembzinfo.getHsbj()+"");
            ncQuotedPrice.setTzxs(jcItembzinfo.getTzxs().floatValue());
            ncQuotedPrice.setDdslxs(jcItembzinfo.getOrderNumCoe().floatValue());
            ncQuotedPrice.setJgndxs(jcItembzinfo.getProcessDiffCoe().floatValue());
            ncQuotedPrice.setBzsl(jcItembzinfo.getLlzl());
            ncQuotedPrice.setFactoryNo("WR");
            ncQuotedPrice.setFactoryName("玮瑞");
            ncQuotedPrice.setOrderNum(jcItembzinfo.getOrderNum());
            ncQuotedPrice.setProductName(jcItembzinfo.getItemName());
            ncQuotedPrice.setCreateBy(SecurityUtils.getUsername());
            ncQuotedPrice.setCreateTime(new Date());
            ncQuotedPrice.setItemIdentifyCode(jcItembzinfo.getItemIdentifyCode());
            ncQuotedPrice.setXlzl(jcItembzinfo.getXlzl());
            ncQuotedPriceMapper.insertNcQuotedPrice(ncQuotedPrice);
        }
        return 1;
    }

    /**
     * 修改报价单
     *
     * @param ncQuotedPrice 报价单
     * @return 结果
     */
    @Override
    public int updateNcQuotedPrice(NcQuotedPrice ncQuotedPrice)
    {
        return ncQuotedPriceMapper.updateNcQuotedPrice(ncQuotedPrice);
    }

    /**
     * 批量删除报价单
     *
     * @param ids 需要删除的报价单主键
     * @return 结果
     */
    @Override
    public int deleteNcQuotedPriceByIds(Long[] ids)
    {
        return ncQuotedPriceMapper.deleteNcQuotedPriceByIds(ids);
    }

    /**
     * 删除报价单信息
     *
     * @param id 报价单主键
     * @return 结果
     */
    @Override
    public int deleteNcQuotedPriceById(Long id)
    {
        return ncQuotedPriceMapper.deleteNcQuotedPriceById(id);
    }

    @Override
    public int scht(NcQuotedPrice ncQuotedPrice) {
        NcContract nc = ncContractMapper.getIseExistBjdh(ncQuotedPrice.getBjdH());
        NcContract ncContract = new NcContract();
        if(nc!=null){
            if("0".equals(ncQuotedPrice.getIsHs())){
                ncContract.setContractUnitPrice(nc.getContractUnitPrice().add(new BigDecimal(ncQuotedPrice.getSjbj()).multiply(new BigDecimal(ncQuotedPrice.getOrderNum()))));
            }else{
                ncContract.setContractUnitPrice(nc.getContractUnitPrice().add(new BigDecimal(ncQuotedPrice.getHsbj()).multiply(new BigDecimal(ncQuotedPrice.getOrderNum()))));
            }
            ncContract.setUuid(nc.getUuid());
            ncContractMapper.updateNcContract(ncContract);
        }else{
            ncContract.setUuid(UUID.randomUUID().toString());
            ncContract.setGcCode("WR");
            ncContract.setGcName("玮瑞");
            ncContract.setContractId("HT"+ NewNumber.createNewNumber(ncQuotedPriceMapper.getNewCode(DateUtils.getDate())));
            ncContract.setItem(ncQuotedPrice.getProductNum());
            ncContract.setItemName(ncQuotedPrice.getProductName());
            if("0".equals(ncQuotedPrice.getIsHs())){
                ncContract.setContractUnitPrice(new BigDecimal(ncQuotedPrice.getSjbj()).multiply(new BigDecimal(ncQuotedPrice.getOrderNum())));
            }else{
                ncContract.setContractUnitPrice(new BigDecimal(ncQuotedPrice.getHsbj()).multiply(new BigDecimal(ncQuotedPrice.getOrderNum())));
            }
            ncContract.setHbCode(ncQuotedPrice.getUnit());
            ncContract.setCreateBy(SecurityUtils.getUsername());
            ncContract.setBjdH(ncQuotedPrice.getBjdH());
            ncContract.setCreateTime(new Date());
            ncContract.setCreateBy(SecurityUtils.getUsername());
            ncContract.setRemark(Constants.HTSM);
            ncContractMapper.insertNcContract(ncContract);
        }
        ncQuotedPrice.setSchtbj("1");
        return ncQuotedPriceMapper.updateNcQuotedPrice(ncQuotedPrice);
    }

    @Override
    public String genBjNumber() {
        return  "TKBJ"+NewNumber.createNewNumber(ncQuotedPriceMapper.getBjCode(DateUtils.getDate()));
    }

    @Override
    public List<NcQuotedPrice> getQPricesByIds(String ids) {
        return ncQuotedPriceMapper.getQPricesByIds(ids);
    }

    @Override
    public String imporIteminfo(List<NcQuotedPrice> priceList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(priceList) || priceList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcQuotedPrice price : priceList)
        {
            try
            {
                BeanValidators.validateWithException(validator, price);
                price.setCreateBy(operName);
                price.setFactoryNo("WR");
                price.setFactoryName("玮瑞");
                price.setUpdateBy(operName);
                this.insertNcQuotedPrice(price);
                successNum++;
                successMsg.append("<br/>" + successNum + "、报价单 " + price.getBjdH()+ " 导入成功");
               /* if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setCreateBy(operName);
                    iteminfo.setUpdateBy(operName);
                    iteminfo.setTenantId(SecurityUtils.getDeptId()+"");
                    this.insertJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标准 " + iteminfo.getItemBzName() + " 导入成功");
                }
                else
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setUpdateBy(operName);
                    this.updateJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + iteminfo.getItemBzName()+ " 更新成功");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + price.getBjdH() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
